home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / CALLING_ / EXTNAME.C < prev    next >
Text File  |  1990-04-08  |  2KB  |  64 lines

  1. /*
  2.  * Example of calling C functions by their names.  Here it's just
  3.  *  chdir (change directory) or getwd (get path of current working directory).
  4.  */
  5.  
  6. #include "../h/config.h"
  7. #include "../h/rt.h"
  8. #include "rproto.h"
  9.  
  10. struct descrip retval;            /* for returned value */
  11.  
  12. dptr extcall(dargv, argc, ip)
  13. dptr dargv;
  14. int argc;
  15. int *ip;
  16.    {
  17.    int len, retcode;
  18.    char sbuf1[MaxCvtLen];        /* for conversion on non-strings */
  19.    char sbuf2[MaxCvtLen];        /* for C-style string */
  20.    int chdir(), getwd();
  21.     
  22.    *ip = -1;                /* anticipate error-free execution */
  23.    
  24.    if (cvstr(dargv, sbuf1) == CvtFail) {  /* 1st argument must be a string */
  25.       *ip = 103;            /* "string expected" error number */
  26.       return dargv;            /* return offending value */
  27.       }
  28.  
  29.    if (strncmp("chdir", StrLoc(*dargv), StrLen(*dargv)) == 0) {
  30.       if (argc < 2) {            /* must be a 2nd argument */
  31.          *ip = 103;            /* no error number fits, really */
  32.          return NULL;            /* no offedning value */
  33.          }
  34.       dargv++;                /* get to next argument */
  35.       if (cvstr(dargv, sbuf1) == CvtFail) {  /* 2nd argument must be a string */
  36.          *ip = 103;            /* "string expected" error number */
  37.          return dargv;            /* return offending value */
  38.          }
  39.       qtos(dargv,sbuf2);        /* get C-style string in sbuf2 */
  40.       retcode = chdir(sbuf2);        /* try to change directory */
  41.       if (retcode == -1)        /* see if chdir failed */
  42.          return (dptr)NULL;        /* signal failure */
  43.       return &zerodesc;            /* not a very useful result */
  44.       }
  45.    else if (strncmp("getwd", StrLoc(*dargv), StrLen(*dargv)) == 0) {
  46.       dargv++;                /* get to next argument */
  47.       retcode = getwd(sbuf2);        /* get current working directory */
  48.       if (retcode == 0)            /* see if getwd failed */
  49.          return NULL;            /* signal failure */
  50.       len = strlen(sbuf2);        /* length of resulting string */
  51.       if (strreq(len) == Error) {    /* need to allocate a copy of result */
  52.          *ip = 0;            /* zero since code is set elsewhere */
  53.          return (dptr)NULL;        /* no offending value */
  54.          }
  55.       StrLoc(retval) = alcstr(sbuf2,len);  /* allocate and copy the string */
  56.       StrLen(retval) = len;
  57.       return &retval;            /* return a pointer to the qualifier */
  58.       }
  59.    else {
  60.       *ip = 216;        /* name is not one of those supported here */
  61.       return dargv;        /* return pointer to offending value */
  62.       }
  63.    }
  64.